package com.xzy.demo.juc.locks;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class AirCondition {
    private int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public synchronized void increment() throws InterruptedException {
        // 1.判断  必须用while判断，绝对不要用if，不然会有虚假唤醒问题
        while (number != 0) {
            this.wait();
        }
        // 2.干活
        number++;
        System.out.println(Thread.currentThread().getName() + " " + number);
        // 3.通知
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        // 1.判断  通知必须用while判断，绝对不要用if，不然会有虚假唤醒问题
        while (number == 0) {
            this.wait();
        }
        // 2.干活
        number--;
        System.out.println(Thread.currentThread().getName() + " " + number);
        // 3.通知
        this.notifyAll();
    }

    public void increment1() {
        lock.lock();
        try {

            // 判断
            while (number != 0) {
                condition.await();
            }

            // 工作
            number++;
            System.out.println(Thread.currentThread().getName() + " " + number);

            // 通知
            condition.signalAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement1() {
        lock.lock();
        try {

            // 判断
            while (number == 0) {
                condition.await();
            }

            // 工作
            number--;
            System.out.println(Thread.currentThread().getName() + " " + number);

            // 通知
            condition.signalAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

/**
 * @author xzy
 * @date 2021-03-22 12:32
 */
public class ProdConsumerDemo04 {
    /**
     * 1. 高聚低合前提下，线程 操作 资源类
     * 2. 判断 干活 通知
     * 3. 防止虚假唤醒：通知必须用while判断，绝对不要用if（Object::wait源码注解）
     */
    public static void main(String[] args) {
        test1();
    }

    /**
     * 题目：两个线程共同操作一个初始值为0的变量，
     * 线程A对该变量+1，线程B对该变量-1，实现10
     * 轮交替修改，变量值为0。
     */
    private static void test0() {
        // 资源类
        AirCondition airCondition = new AirCondition();

        // 线程 操作
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
    }

    /**
     * 题目：四个线程共同操作一个初始值为0的变量，
     * 线程AB对该变量+1，线程CD对该变量-1，实现10
     * 轮交替修改，变量值为0。
     */
    private static void test1() {
        // 资源类
        AirCondition airCondition = new AirCondition();

        // 线程 操作
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.increment1();
            }
        }, "+").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.increment1();
            }
        }, "+`").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.decrement1();
            }
        }, "-").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.decrement1();
            }
        }, "-`").start();
    }

}


